/**
 * 
 * Copyright 2009 Marco Speranza <marco.speranza79@gmail.com>
 *    
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *   
 * http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.commons.database.util.jquerycreator;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

// TODO: Auto-generated Javadoc
/**
 * Crea una instanza sincronizzata di <code>DatabaseQuaryCreator</code>.
 * 
 * @author Marco Speranza
 */
public class SyncronizedDataBaseQueryCreator implements IDataBaseQueryCreator {

    /** The data base query creator. */
    private IDataBaseQueryCreator dataBaseQueryCreator;

    /** The mutex. */
    private Object mutex;

    /**
     * Costruisce una classe sincronizzata.
     * 
     * @param dataBaseQueryCreator
     *            l'instanza da sincronizzare
     */
    public SyncronizedDataBaseQueryCreator(
	    IDataBaseQueryCreator dataBaseQueryCreator) {
	super();
	if (dataBaseQueryCreator == null)
	    throw new NullPointerException();
	this.dataBaseQueryCreator = dataBaseQueryCreator;
	mutex = this;
    }

    /**
     * Costruisce una classe sincronizzata.
     * 
     * @param dataBaseQueryCreator
     *            l'instanza da sincronizzare
     * @param mutex
     *            il lock
     */
    public SyncronizedDataBaseQueryCreator(
	    IDataBaseQueryCreator dataBaseQueryCreator, Object mutex) {
	super();
	if (dataBaseQueryCreator == null)
	    throw new NullPointerException();
	this.dataBaseQueryCreator = dataBaseQueryCreator;
	this.mutex = mutex;
    }
    
    
    

    /* (non-Javadoc)
     * @see org.commons.database.util.jquerycreator.IDataBaseQueryCreator#commit()
     */
    public void commit() throws SQLException {
	synchronized (mutex) {
	    dataBaseQueryCreator.commit();
	}// TODO Auto-generated method stub
	
    }


    /*
     * (non-Javadoc)
     * 
     * @see
     * org.commons.database.util.jquerycreator.IDataBaseQueryCreator#clear()
     */
    public void clear() {
	synchronized (mutex) {
	    dataBaseQueryCreator.clear();
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.commons.database.util.jquerycreator.IDataBaseQueryCreator#closeAll()
     */
    public void closeAll() throws SQLException {
	synchronized (mutex) {
	    dataBaseQueryCreator.closeAll();
	}

    }
    
    

    /* (non-Javadoc)
     * @see org.commons.database.util.jquerycreator.IDataBaseQueryCreator#closeAllWithoutConnection()
     */
    public void closeAllWithoutConnection() throws SQLException {
	synchronized (mutex) {
	    dataBaseQueryCreator.closeAllWithoutConnection();
	}	
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.commons.database.util.jquerycreator.IDataBaseQueryCreator#executeQuery
     * ()
     */
    public void executeQuery() throws SQLException {
	synchronized (mutex) {
	    dataBaseQueryCreator.executeQuery();
	}

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.commons.database.util.jquerycreator.IDataBaseQueryCreator#executeUpdate
     * ()
     */
    public int executeUpdate() throws SQLException {
	synchronized (mutex) {
	    return dataBaseQueryCreator.executeUpdate();
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.commons.database.util.jquerycreator.IDataBaseQueryCreator#executeUpdate
     * (int[])
     */
    public int executeUpdate(int[] columnIndex) throws SQLException {
	synchronized (mutex) {
	    return dataBaseQueryCreator.executeUpdate(columnIndex);
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.commons.database.util.jquerycreator.IDataBaseQueryCreator#fillQuery()
     */
    public void fillQuery() throws SQLException {
	synchronized (mutex) {
	    dataBaseQueryCreator.fillQuery();
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.commons.database.util.jquerycreator.IDataBaseQueryCreator#fillQuery
     * (java.sql.PreparedStatement)
     */
    public PreparedStatement fillQuery(PreparedStatement stmt)
	    throws SQLException {
	synchronized (mutex) {
	    return dataBaseQueryCreator.fillQuery(stmt);
	}

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.commons.database.util.jquerycreator.IDataBaseQueryCreator#fillQuery
     * (java.lang.String)
     */
    public void fillQuery(String sql) throws SQLException {
	synchronized (mutex) {
	    dataBaseQueryCreator.fillQuery(sql);
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.commons.database.util.jquerycreator.IDataBaseQueryCreator#getFilledQuery
     * ()
     */
    public String getFilledQuery() {
	synchronized (mutex) {
	    return dataBaseQueryCreator.getFilledQuery();
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.commons.database.util.jquerycreator.IDataBaseQueryCreator#getPosition
     * ()
     */
    public int getPosition() {
	synchronized (mutex) {
	    return dataBaseQueryCreator.getPosition();
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.commons.database.util.jquerycreator.IDataBaseQueryCreator#getResultSet
     * ()
     */
    public ResultSet getResultSet() {
	synchronized (mutex) {
	    return dataBaseQueryCreator.getResultSet();
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.commons.database.util.jquerycreator.IDataBaseQueryCreator#getSql()
     */
    public String getSql() {
	synchronized (mutex) {
	    return dataBaseQueryCreator.getSql();
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @seeorg.commons.database.util.jquerycreator.IDataBaseQueryCreator#
     * makePreparedStatement()
     */
    public PreparedStatement makePreparedStatement() throws SQLException {
	synchronized (mutex) {
	    return dataBaseQueryCreator.makePreparedStatement();
	}
    }

    /*
     * (non-Javadoc)
     * 
     * @seeorg.commons.database.util.jquerycreator.IDataBaseQueryCreator#
     * makePreparedStatement(java.lang.String)
     */
    public PreparedStatement makePreparedStatement(String sql)
	    throws SQLException {
	synchronized (mutex) {
	    return dataBaseQueryCreator.makePreparedStatement();
	}
    }

    
   

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.commons.database.util.jquerycreator.IDataBaseQueryCreator#put(java
     * .lang.Object, int)
     */
    public DataBaseQueryCreatorBean put(Object value, int type) {
	synchronized (mutex) {
	    return dataBaseQueryCreator.put(value, type);
	}
    }

   

    /*
     * (non-Javadoc)
     * 
     * @seeorg.commons.database.util.jquerycreator.IDataBaseQueryCreator#
     * setDataSourceManager
     * (org.commons.database.util.jquerycreator.IDatasourceManager)
     */
    public void setDataSourceManager(IDatasourceManager datasourceManager) {
	synchronized (mutex) {
	    dataBaseQueryCreator.setDataSourceManager(datasourceManager);
	}

    }

    /*
     * (non-Javadoc)
     * 
     * @see
     * org.commons.database.util.jquerycreator.IDataBaseQueryCreator#setSql(
     * java.lang.String)
     */
    public void setSql(String sql) {
	synchronized (mutex) {
	    dataBaseQueryCreator.setSql(sql);
	}

    }

    /*
     * (non-Javadoc)
     * 
     * @seeorg.commons.database.util.jquerycreator.IDataBaseQueryCreator#
     * setAutogeneratedKey(int)
     */
    public void setAutogeneratedKey(int autogeneratedKey) {
	synchronized (mutex) {
	    dataBaseQueryCreator.setAutogeneratedKey(autogeneratedKey);
	}

    }

}
